package cn.shadow.game.chess.CHnchess.chessman;


import cn.shadow.game.chess.CHnchess.board.Board;
import cn.shadow.game.chess.CHnchess.board.Position;
import cn.shadow.game.chess.CHnchess.board.Slash;
import cn.shadow.game.chess.CHnchess.type.Camp;
import cn.shadow.game.chess.CHnchess.type.ChessType;

public class Xiang extends AbstractChess implements Slash {
    public Xiang(Board board, Camp camp) {
        super(ChessType.XIANG, board, camp);
    }

    @Override
    public void addLeftUpPoint() {
        int row = this.getRow();
        int col = this.getCol();
        Position center = getBoard().getPoint(row - 1, col - 1);
        Position position = getBoard().getPoint(row - 2, col - 2);
        if (barrier(center)) {
            return;
        }
        if (position != null) {
            addPosition(position);
        }

    }


    @Override
    public void addRightUpPoint() {
        int row = this.getRow();
        int col = this.getCol();
        Position center = getBoard().getPoint(row - 1, col + 1);
        Position point = getBoard().getPoint(row - 2, col + 2);
        if (barrier(center)) {
            return;
        }

        if (point != null) {
            addPosition(point);
        }
    }


    @Override
    public void addLeftDownPoint() {
        int row = this.getRow();
        int col = this.getCol();
        Position center = getBoard().getPoint(row + 1, col - 1);
        Position point = getBoard().getPoint(row + 2, col - 2);
        if (barrier(center)) {
            return;
        }
        if (point != null) {
            addPosition(point);
        }
    }


    @Override
    public void addRightDownPoint() {
        int row = this.getRow();
        int col = this.getCol();
        Position center = getBoard().getPoint(row + 1, col + 1);
        Position point = getBoard().getPoint(row + 2, col + 2);
        if (barrier(center)) {
            return;
        }
        if (point != null) {
            addPosition(point);
        }
    }

    private void addPosition(Position point) {
        Chess chess = point.getChess();
        if (chess != null) {
            if (chess.getCamp() == this.getCamp()) {
                return;
            }
        }
        if (point.isOverRanged()) {
            return;
        }
        if (isPassRiver(point)) {
            return;
        }
        canSetPosition.add(point);
        canAttackPosition.add(point);
    }

    private boolean barrier(Position point) {
        if (point == null) {
            return false;
        }
        Chess chess = point.getChess();
        if (chess == null) {
            return false;
        }
        return true;
    }

    /**
     * 黑方过河的条件是:
     * 阵营是黑色,然后所在的点的行坐标小于4 row<4
     * 红色过河的条件
     *
     * @param point
     * @return
     */
    private boolean isPassRiver(Position point) {
        return this.getCamp() == Camp.BLACK && !point.isDown()
                || this.getCamp() == Camp.RED && point.isDown();

    }
}
